package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Autowired
    private WmNewsService wmNewsService;

    /**
     * 查询所有频道
     * @return
     */
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    /**
     * 新增频道
     * @param adChannel
     * @Params:
     * @Return 1
     */
    @Override
    public ResponseResult saveChannel(WmChannel adChannel) {
        //参数校验
        if (adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据频道名称查询频道
        WmChannel wmChannel = getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, adChannel.getName()));
        if (wmChannel!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //数据操作
        adChannel.setCreatedTime(new Date());
        save(adChannel);
        //数据返回
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除频道
     * @param id
     * @Params:
     * @Return
     */
    @Override
    public ResponseResult delChannel(Integer id) {
        //参数校验
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询频道
        WmChannel wmChannel = getById(id);
        if (wmChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断是否有效 启用/禁用
        if (wmChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断是否引用
        int count = wmNewsService.count(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, wmChannel.getId())
                .eq(WmNews::getStatus, WmNews.Status.PUBLISHED.getCode()));
        if (count>0){
            //当count>0 说明此频道正在使用,无法删除
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //没有任何关联 可以删除 返回结果
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 频道分页
     * @param dto
     * @Params:
     * @Return
     */
    @Override
    public ResponseResult channelList(WmChannelDto dto) {
        //参数校验
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查分页条件
        dto.checkParam();
        //查询条件
        LambdaQueryWrapper<WmChannel> channelWrapper = Wrappers.<WmChannel>lambdaQuery();
        //处理状态条件
        if (StringUtils.isNotBlank(dto.getName())){
            channelWrapper.like(WmChannel::getName,dto.getName());
        }

        //分页
        IPage ipage = new Page(dto.getPage(), dto.getSize());
        //组装
        page(ipage,channelWrapper);

        //4. 封装结果返回
        List list = ipage.getRecords();     //当前页数据
        long total = ipage.getTotal();      //总记录条数
        ResponseResult result = new PageResponseResult((int) ipage.getCurrent(),
                (int) ipage.getSize(), (int) total);
        result.setData(list);
        return result;
    }

    /**
     * 修改频道
     * @param wmChannel
     * @Params:
     * @Return
     */
    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {
        //参数校验
        if (wmChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //数据操作
        //判断是否引用
        int count = wmNewsService.count(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, wmChannel.getId())
                .eq(WmNews::getStatus, WmNews.Status.PUBLISHED.getCode()));
        if (count>0){
            //当count>0 说明此频道正在使用,无法修改
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        updateById(wmChannel);
        //返回数据
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}